package org.sdnstproject.interlayer.OF13Protocal;

import java.util.Arrays;
import java.util.Random;

import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuQueueGetConfigReply {
	private static final Logger log = LoggerFactory.getLogger(FuQueueGetConfigReply.class);
    public byte version = (byte)4;
    public byte type = (byte)23;
    public short length = (short)16;// MINIMUM_LENGTH = 16;
    public long xid = 0x0L;
	public long port = 0xFFffFFff;//OFPort DEFAULT_PORT = OFPort.ANY;OFPP_ANY_INT = 0xFFffFFff;
	public byte[] queues = new byte[] {};
	
	public FuQueueGetConfigReply() {
		
	}
	    	
    public FuQueueGetConfigReply setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "port":
			this.port = Long.parseLong(value);
			break;
		case "queues":
			this.queues = new byte[(new Random().nextInt(100))];
			new Random().nextBytes(queues);
			break;
		}
		return this;
	}
    
	public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
		int startIndex = bb.writerIndex();
        // fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 22
        bb.writeByte(type);
        // length is length of variable message, will be updated at the end
        int lengthIndex = bb.writerIndex();
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        bb.writeInt(U32.t(port));
        // pad: 4 bytes
        bb.writeZero(4);
        bb.writeBytes(queues);

        // update length field
        length = (short)(bb.writerIndex() - startIndex);
        bb.setShort(lengthIndex, length);
        return bb;
    }
	@Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFQueueGetConfigReplyVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("port=").append(port);
        b.append(",");
        b.append("queues=").append(Arrays.toString(queues));
        b.append(")");
        return b.toString();
    }
}
